package com.jmb.api.common.exception;

import com.jmb.api.common.result.Result;


import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * 全局异常处理
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Result error(Exception e){
        log.error("捕获到异常信息：", e);
        return Result.fail();
    }
    @ExceptionHandler(WebApiException.class)
    @ResponseBody
    public Result error(WebApiException e){
        log.error("捕获到的异常信息：",e);
        return Result.fail(e.getCode(),e.getMessage());
    }
    @ExceptionHandler(JsonException.class)
    @ResponseBody
    public Result error(JsonException e){
        log.error("捕获到的异常信息：",e);
        return Result.fail(e.getMsg());
    }

    @ExceptionHandler(ValidUtilException.class)
    @ResponseBody
    public Result error(ValidUtilException e){
        log.error("捕获到的异常信息：",e);
        return Result.fail(e.getMessage());
    }
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result handleValidationException(MethodArgumentNotValidException e){
        log.info("参数校验失败");
        return Result.fail("参数校验失败："+e.getBindingResult().getFieldError().getDefaultMessage());
    }
    @ExceptionHandler(ParseException.class)
    public Result handleParseException(ParseException e){
        log.info("解析异常");
        return Result.fail("解析异常："+e.getMessage());
    }
}
